﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using Schedule.UI.ScheduleService;
using Schedule.UI.Foundation;
using System.Windows.Input;

namespace Schedule.UI.ViewModel
{
    public class CTDTViewModel : INotifyPropertyChanged
    {
        public CTDTViewModel()
        {
            RefreshCTDT();
            RefreshMonhoc();
            RefreshKhoa();
            RefreshHocky();
            this.PropertyChanged += (s, e) => {
                if (insertCurCTDT != null)
                    insertCurCTDT.RaiseCanExecuteChanged();
                if (saveCurCTDT != null)
                    saveCurCTDT.RaiseCanExecuteChanged();
                if (delCurCTDT != null)
                    delCurCTDT.RaiseCanExecuteChanged();
            };
            
        }
        #region Khai bao
        public ScheduleServiceClient ServiceClient = new ScheduleServiceClient();
        #region propchange
        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChange(string propertyName)
        {
            if (PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
        #endregion
        private IEnumerable<CTDT> cTDTs;
        public IEnumerable<CTDT> CTDTs
        {
            get { return this.cTDTs; }
            set
            {
                this.cTDTs = value;
                OnPropertyChange("CTDTs");
            }
        }

        private CTDT currentCTDT = new CTDT();
        public CTDT CurrentCTDT
        {
            get { return this.currentCTDT; }
            set
            {
                this.currentCTDT = value;
                OnPropertyChange("CurrentCTDT");
                if (this.CurrentCTDT != null && this.CurrentCTDT.ID != 0)
                {
                    CurrentMonhoc = Monhocs.Where(x => x.ID == CurrentCTDT.MonHoc_ID).First();
                    CurrentHocky = Hockys.Where(x => x.ID == CurrentCTDT.HocKy_ID).First();
                    CurrentKhoa = Khoas.Where(x => x.ID == CurrentCTDT.Khoa_ID).First();
                }
            }
        }

        private IEnumerable<MONHOC> monhocs;
        public IEnumerable<MONHOC> Monhocs
        {
            get { return this.monhocs; }
            set
            {
                this.monhocs = value;
                OnPropertyChange("Monhocs");
                if (this.Monhocs != null && SelectedMonhoc != null && SelectedMonhoc.ID != 0)
                    CurrentMonhoc = Monhocs.Where(x => x.ID == SelectedMonhoc.ID).First();
            }
        }
        private  MONHOC currentMonhoc= new MONHOC();
        public  MONHOC CurrentMonhoc
        {
            get { return this.currentMonhoc; }
            set
            {
                this.currentMonhoc = value;
                OnPropertyChange("CurrentMonhoc");
            }
        }
        private static MONHOC selectedMonhoc = new MONHOC();
        public static MONHOC SelectedMonhoc
        {
            get { return selectedMonhoc; }
            set
            {
                selectedMonhoc = value;
            }
        }
        private IEnumerable<HOCKY> hockys;
        public IEnumerable<HOCKY> Hockys
        {
            get { return this.hockys; }
            set
            {
                this.hockys = value;
                OnPropertyChange("Hockys");
                if (this.Hockys != null && CurrentHocky != null && CurrentHocky.ID == 0)
                    CurrentHocky = Hockys.First();
            }
        }

        private HOCKY currentHocky = new HOCKY();
        public HOCKY CurrentHocky
        {
            get { return this.currentHocky; }
            set
            {
                this.currentHocky = value;
                OnPropertyChange("CurrentHocky");
            }
        }

        private IEnumerable<KHOA> khoas;
        public IEnumerable<KHOA> Khoas
        {
            get { return this.khoas; }
            set
            {
                this.khoas = value;
                OnPropertyChange("Khoas");
                if (this.Khoas != null && CurrentKhoa != null && CurrentKhoa.ID == 0)
                    CurrentKhoa = Khoas.First();
            }
        }

        private KHOA currentKhoa = new KHOA();
        public KHOA CurrentKhoa
        {
            get { return this.currentKhoa; }
            set
            {
                this.currentKhoa = value;
                OnPropertyChange("CurrentKhoa");
            }
        }

        #endregion

        #region refreshCTDT

        private void RefreshCTDT()
        {
            this.ServiceClient.getAllCTDTCompleted += (s, e) =>
            {
                this.CTDTs = e.Result;
            };
            this.ServiceClient.getAllCTDTAsync();
        }

        private void RefreshMonhoc()
        {
            this.ServiceClient.getAllMonhocCompleted += (s, e) =>
            {
                this.Monhocs = e.Result;
            };
            this.ServiceClient.getAllMonhocAsync();
        }
        public void RefreshCurMonhoc()
        {
            if(Monhocs!=null && CurrentMonhoc!=null && CurrentMonhoc.ID!=0)
            CurrentMonhoc = Monhocs.Where(x => x.ID == CurrentMonhoc.ID).First();
        }
        private void RefreshKhoa()
        {
            //throw new NotImplementedException();
            this.ServiceClient.getAllKhoaCompleted += (s, e) =>
            {
                this.Khoas = e.Result;
            };
            this.ServiceClient.getAllKhoaAsync();
         }

        private void RefreshHocky()
        {
            //throw new NotImplementedException();
            this.ServiceClient.getAllHockyCompleted += (s, e) =>
            {
                this.Hockys = e.Result;
            };
            this.ServiceClient.getAllHockyAsync();
        }

        #endregion
        
        #region Save
        private RelayCommand saveCurCTDT;
        public ICommand SaveCurCTDT
        {
            get { return saveCurCTDT ?? (saveCurCTDT = new RelayCommand(() => SaveCTDT(), () => this.currentCTDT != null && this.currentCTDT.ID != 0)); }
        }

        private void SaveCTDT()
        {
            this.CurrentCTDT.HocKy_ID = CurrentHocky.ID;
            this.CurrentCTDT.Khoa_ID = CurrentKhoa.ID;
            this.CurrentCTDT.MonHoc_ID = CurrentMonhoc.ID;
            ServiceClient.saveCTDTAsync(currentCTDT);
            RefreshCTDT();
        }

        
        #endregion

        #region del
        private RelayCommand delCurCTDT;
        public ICommand DelCurCTDT
        {
            get { return delCurCTDT ?? (delCurCTDT = new RelayCommand(() => DelCTDT(), () => this.currentCTDT != null && this.currentCTDT.ID != 0)); }
        }

        private void DelCTDT()
        {
            ServiceClient.deleteCTDTAsync(currentCTDT);
            RefreshCTDT();
        }
        #endregion

        #region Insert

        private RelayCommand insertCurCTDT;
        public ICommand InsertCurCTDT
        {
            get { return insertCurCTDT ?? (insertCurCTDT = new RelayCommand(() => InsertCTDT(), () => this.currentCTDT != null)); }
        }

        private void InsertCTDT()
        {
            this.CurrentCTDT.HocKy_ID = CurrentHocky.ID;
            this.CurrentCTDT.Khoa_ID = CurrentKhoa.ID;
            this.CurrentCTDT.MonHoc_ID = CurrentMonhoc.ID;
            ServiceClient.insertCTDTAsync(CurrentCTDT);
            RefreshCTDT();
        }
        #endregion
        
    }
}
